package am.NotenProgramm.Frame.Panels;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import am.NotenProgramm.Frame.GradePro;
import am.NotenProgramm.Frame.NewGradeFrame;
import am.NotenProgramm.objects.Subject;
import am.NotenProgramm.objects.myDate;

public class GradeMainPanel extends JPanel implements ActionListener{
	
	//table
	JPanel tablePanel = new JPanel();
	JScrollPane tablePane;
	
	//buttons
	JButton buttonNewGrade = new JButton(GradePro.rb.getString("buttonNewGrade"));
	JButton buttonSettingsGrade = new JButton(GradePro.rb.getString("buttonSettings"));
	JButton buttonIneed = new JButton(GradePro.rb.getString("buttonIneed"));
	
	JPanel panelButtons = new JPanel();
	
	JTable tableGrades;
	
	String[] tableHeader = {GradePro.rb.getString("tableName"),GradePro.rb.getString("tableGrade"),GradePro.rb.getString("tableIsOral"),GradePro.rb.getString("gradeWeight"),GradePro.rb.getString("tableDate")};
	Object[][] tableContent;
	Subject subject;

	public GradeMainPanel(Subject subject, Dimension size) {
		setSize(size);
		setLayout(null);
		this.subject = subject;
		
		tableContent = new Object[subject.getGrades().length][5];
		for(int i = 0; i < subject.getGrades().length; i++){
			tableContent[i][0] = subject.getGrades()[i].getName();
			tableContent[i][1] = subject.getGrades()[i].getGrade();
			tableContent[i][2] = subject.getGrades()[i].isOral(); 
			tableContent[i][3] = subject.getGrades()[i].getWeight(); 
			tableContent[i][4] = (subject.getGrades()[i].getDate().getDay() + "."
			+ subject.getGrades()[i].getDate().getMonth() + "." + subject.getGrades()[i].getDate().getYear());
		}
		
		
		//table
		tablePanel.setSize((getWidth()),(int)(getHeight()*0.9));
		tablePanel.setLocation(0, 0);
		tablePanel.setBorder(BorderFactory.createLoweredSoftBevelBorder());
		add(tablePanel);
		tableGrades = new JTable(tableContent,tableHeader);
		tablePane = new JScrollPane(tableGrades,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		tablePanel.add(tablePane);
		tableGrades.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				saveGrades();
				if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE){
					if (JOptionPane
							.showConfirmDialog(
									new JFrame(),
									GradePro.rb.getString("reallyDelete") + "?",
									"Warning", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
						deleteGrade();
					}
					e.consume();
				}
			}
		});
		tableGrades.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				saveGrades();
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		//buttonPanel
		panelButtons.setSize((getWidth()),(int)(getHeight()*0.1));
		panelButtons.setLocation(0,(int)(getHeight()*0.9));
		panelButtons.setBorder(BorderFactory.createLoweredSoftBevelBorder());
		panelButtons.setLayout(new GridLayout(1, 3));
		add(panelButtons);
		
	
		panelButtons.add(buttonNewGrade); 	
		buttonNewGrade.setActionCommand("newGrade");
		buttonNewGrade.addActionListener(this);
		
		panelButtons.add(buttonSettingsGrade); 
		buttonSettingsGrade.setActionCommand("settings");
		buttonSettingsGrade.addActionListener(this);
		buttonSettingsGrade.setEnabled(false);
		
		panelButtons.add(buttonIneed);  
		buttonIneed.setActionCommand("iNeed");
		buttonIneed.addActionListener(this);
		buttonIneed.setEnabled(false);
	}
	
	//Save Contentchanges to the File
	public void saveGrades () {
		// Apply Changes to the internal Subject
		for(int i = 0; i < tableContent.length; i++){
			subject.getGrades()[i].setName(tableContent[i][0].toString());
			subject.getGrades()[i].setGrade(Double.parseDouble(tableContent[i][1].toString()));
			if(tableContent[i][2].toString().equals("true") || tableContent[i][2].toString().equals("y") || tableContent[i][2].toString().equals("ja")) {
				subject.getGrades()[i].setOral(true);
			} else {
				subject.getGrades()[i].setOral(false);
			}
			subject.getGrades()[i].setWeight(Double.parseDouble(tableContent[i][3].toString())); 
			subject.getGrades()[i].setDate(new myDate(tableContent[i][4].toString()));
		}
		
		// Save the internal Subject into myProfile
		for(int i = 0; i < GradePro.myProfile.getSubjects().length; i++) {
			if(GradePro.myProfile.getSubjects()[i].getName().equals(this.subject.getName())) {
				GradePro.myProfile.setSubject(subject, i);
				GradePro.myStream.saveProfile(GradePro.myProfile);
			}
		}
		GradePro.mainFrame.panelGrades.refreshInfoBox();
		GradePro.mainFrame.panelStatistics.refresh();
	}
	
	private void deleteGrade() {
		// TODO Auto-generated method stub
		saveGrades();
		this.subject.removeGrade(tableGrades.getSelectedRow());
		int tmpSelectedGradeValue = GradePro.mainFrame.panelGrades.listSubjects.getSelectedIndex();
		GradePro.mainFrame.panelGrades.refreshPanels();
		GradePro.mainFrame.panelStatistics.refresh();
		GradePro.mainFrame.panelGrades.listSubjects.setSelectedIndex(tmpSelectedGradeValue);
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getActionCommand().equals("newGrade")) {
			NewGradeFrame frameNewGrade = new NewGradeFrame();
			GradePro.mainFrame.setEnabled(false);
		}
		if (event.getActionCommand().equals("iNeed")) {

		}
		if(event.getActionCommand().equals("settings")){

		}		
	}

	public Subject getSubject() {
		return subject;
	}


}
